package com.dream.cgomall.controller;

import com.dream.cgomall.dto.*;
import com.dream.cgomall.entity.CgoAdmin;
import com.dream.cgomall.entity.CgoAdminAddress;
import com.dream.cgomall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * (CgoAdmin)表控制层
 *
 * @author makejava
 * @since 2021-07-02 09:39:48
 */
@Controller
@RequestMapping("cgoAdmin")
public class CgoAdminController {
    /**
     * 服务对象
     */
    @Autowired
    private CgoAdminAddressService cgoAdminAddressService;
    @Autowired
    private CgoAdminService cgoAdminService;
    @Autowired
    private SmsService smsService;
    @Autowired
    VerificationTokenService verificationTokensService;

    //通过手机和密码登录
    @RequestMapping("login")
    @ResponseBody
    public ServiceResult<UserDTO> login(String phone, String password) {
        System.out.println(phone + "  " + password);

        return cgoAdminService.Login(phone, password);
    }

    //通过手机验证码登录
    @RequestMapping("Smslogin")
    @ResponseBody
    public ServiceResult Smslogin(String phone, String code) {
        if (smsService.getSmsCode(phone, code).isSuccess()) {
            smsService.remove(phone);
            return cgoAdminService.SmsLogin(phone);
        }
        return ServiceResult.notFound();
    }

    //注册并登录
    @RequestMapping("singUp")
    @ResponseBody
    public ServiceResult<UserDTO> singUp(String phone, String password, String code) {
        return this.cgoAdminService.singUp(new CgoAdmin(phone, password), code);
    }

    // @RequestMapping("deleteUser")
    // public boolean deleteUser(Integer id) {
    //     return this.cgoAdminService.updateDeleteById(id);
    // }
    @RequestMapping("chengePhone")
    @ResponseBody
    public ServiceResult chengePhone(String phone, String code) {
        if (smsService.getSmsCode(phone, code).isSuccess()) {
            return cgoAdminService.chengePhone(phone);
        }
        return smsService.getSmsCode(phone, code);
    }

    //验证功能，查看用户的id和token是否合法
    @RequestMapping("verification")
    @ResponseBody
    public ServiceResult verificationToken(Integer id, String token) {
        return verificationTokensService.verificationToken(id, token);
    }


    //------------------用户个人信息------------------
    //加载用户个人资料
    @RequestMapping("loadUserinfo")
    @ResponseBody
    public ServiceResult loadUserPage(Integer id) {
        return cgoAdminService.loadMyPage(id);
    }

    //获取详细资料
    @RequestMapping("getUserinfo")
    @ResponseBody
    public ServiceResult getUserinfo(Integer id) {
        return cgoAdminService.getUserinfo(id);
    }

    //更新用户信息
    @RequestMapping("updateUserinfo")
    @ResponseBody
    public ServiceResult updateUserinfo(@RequestBody CgoAdmin cgoAdmin) {

        return cgoAdminService.updateUserinfo(cgoAdmin);
    }
//——————————————————————收货地址页面——————————————————————————
    //加载用户地址列表


    //——————————————————————收货地址页面——————————————————————————
    @RequestMapping("loadAddressList")
    @ResponseBody
    public ServiceResult loadAddressList(Integer userId) {
        return cgoAdminAddressService.getUserAddress(userId);
    }

    //修改用户地址
    @RequestMapping("updateAddressList")
    @ResponseBody
    public ServiceResult updateAddressList(@RequestBody CgoAdminAddress cgoAdminAddress) {
        return cgoAdminAddressService.updateUserAddress(cgoAdminAddress);
    }

    //添加用户地址
    @RequestMapping("insertAddressList")
    @ResponseBody
    public ServiceResult insertAddressList(@RequestBody CgoAdminAddress cgoAdminAddress) {
        return cgoAdminAddressService.addUserAddress(cgoAdminAddress);
    }

    //删除用户地址
    @RequestMapping("delAddressList")
    @ResponseBody
    public ServiceResult delAddressList(Integer id) {
        return cgoAdminAddressService.deleteAddress(id);
    }

    @Autowired
    CgoAdminGoodsService cgoAdminGoodsService;

    //——————————————————————历史、关注、收藏————————————————————————
    //获取历史记录
    @RequestMapping("getHistory")
    @ResponseBody
    public ServiceResult getHistory(Integer userId) {
        return cgoAdminGoodsService.getHistoryGoods(userId);
    }

    //获取喜欢文章记录
    @RequestMapping("getMyLikeGoods")
    @ResponseBody
    public ServiceResult getMyLikeGoods(Integer userId) {
        return cgoAdminGoodsService.getMyLikeGoods(userId);
    }

    //获取关注商品记录
    @RequestMapping("getAttention")
    @ResponseBody
    public ServiceResult getAttention(Integer userId) {
        return cgoAdminGoodsService.getAttention(userId);
    }

    //删除喜欢文章记录
    @RequestMapping("delMylike")
    @ResponseBody
    public ServiceResult delMyLike(String[] ids) {
        return cgoAdminGoodsService.NotMylike(Arrays.asList(ids.clone()));
    }

    //删除历史记录
    @RequestMapping("delHistory")
    @ResponseBody
    public ServiceResult delHistory(String[] ids) {

        return cgoAdminGoodsService.NotHistory(Arrays.asList(ids.clone()));
    }

    //获取关注记录
    @RequestMapping("delAttention")
    @ResponseBody
    public ServiceResult delAttention(String[] ids) {
        return cgoAdminGoodsService.NotAttention(Arrays.asList(ids.clone()));
    }

    //清空历史记录
    @RequestMapping("clearAllHistory")
    @ResponseBody
    public ServiceResult clearAllHistory(Integer userId) {
        return cgoAdminGoodsService.ClearHistory(userId);
    }
}
